#!/bin/bash

# MySQL with Docker 部署脚本
echo ==================================================
echo Script startup for deploy MySQL
echo ==================================================

# MySQL镜像库地址
MYSQL_DOCKER_REGISTRY=daocloud.io/library/mysql
# MySQL镜像版本
MYSQL_DOCKER_VERSION=5.7.20
# MySQL的Docker实例名称
MYSQL_DOCKER_INSTANCE_NAME=mysql-main
# MySQL物理机映射端口
MYSQL_PHYSICAL_MACHINE_PORT=3306
# MySQL虚拟机映射端口
MYSQL_VIRTUAL_MACHINE_PORT=3306
# MySQL初始用户名
MYSQL_INIT_USERNAME=ROOT
# MySQL初始密码
MYSQL_INIT_PASSWORD=12345678

############################ installing start ############################
echo -e "-------------------- installing\n"

echo "-------------------- pulling mysql docker image"
docker pull $MYSQL_DOCKER_REGISTRY:$MYSQL_DOCKER_VERSION
echo -e "-------------------- pull success.\n"
sleep 3

echo "-------------------- running mysql"
docker run --name $MYSQL_DOCKER_INSTANCE_NAME -p $MYSQL_PHYSICAL_MACHINE_PORT:$MYSQL_VIRTUAL_MACHINE_PORT -e MYSQL\_$MYSQL_INIT_USERNAME\_PASSWORD=$MYSQL_INIT_PASSWORD -d $MYSQL_DOCKER_REGISTRY:$MYSQL_DOCKER_VERSION
echo -e "-------------------- run success.\n"
echo -e "Initial user name: $MYSQL_INIT_USERNAME, initial password: $MYSQL_INIT_PASSWORD\n"
sleep 3

echo -e "-------------------- install completed!\n"
############################ installing end ##############################

############################ validating start ############################
echo -e "-------------------- validating\n"

echo -e "-------------------- validating mysql run status\n"
docker ps -a | grep "mysql"
sleep 3

echo -e "-------------------- validate completed!\n"
############################ validating end ############################

echo =================================================
echo Deploy MySQL completed!
echo =================================================